package ssx.demo.springbootmysqlcurd.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ssx.demo.springbootmysqlcurd.entity.UserPOJO;
import ssx.demo.springbootmysqlcurd.service.Service;

import java.util.List;

@RestController
@org.springframework.stereotype.Controller
public class Controller {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Service service;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Value("${spring.application.name}")
    private String appName;

    @RequestMapping("/deal/{rest}.do")
//    public String fun(@RequestBody UserPOJO userPOJO, @PathVariable("rest") String path){
    public String fun(@RequestBody String  req, @PathVariable("rest") String path){
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
        System.out.println(sqlSessionTemplate);
//        System.out.println("my-controller-path:"+path);
//        System.out.println("my-controller-path:"+path);

        UserPOJO userPOJO = null;
        try {
            userPOJO = new ObjectMapper().readValue(req, UserPOJO.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        logger.warn("-----------------ssx-service:"+appName+",is request starting,data:"+userPOJO);
        try {


            switch (path) {
                case "c": {
                    boolean b = service.addUser(userPOJO);
                    return b + "创建：" + userPOJO;
                }
                case "u": {
                    boolean b = service.modifyUser(userPOJO);
                    return b + "更改：" + userPOJO;
                }
                case "ro":
                    userPOJO = service.queryUser(userPOJO);
                    return userPOJO.toString();
                case "d": {
                    boolean b = service.removeUser(userPOJO);
                    return b + "删除：" + userPOJO;
                }
                case "ra":
                    List<UserPOJO> userPOJOS = service.queryAll();
                    return userPOJOS.toString();
            }

            return "失败！！";
        }finally {
            logger.warn("-----------------ssx-service:"+appName+",is request end,data:"+userPOJO);
        }

    }

}
